package br.com.leilao.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.faces.context.FacesContext;

import org.primefaces.model.UploadedFile;

import br.com.leilao.dto.ProdutoDTO;
import br.com.leilao.model.Produto;

public class FileUtil {

	public static void copiarArquivoPServidor(UploadedFile uploadFile,String usuario) {
		try {  
	        InputStream in = new BufferedInputStream(uploadFile.getInputstream());  
	        File file = new File("F:/server/tomcat/wtpwebapps/william_flavio/resources/"+usuario);  
  
	        if(!file.exists()){
	        	file.mkdirs();
	        }
            
            FileOutputStream fout = new FileOutputStream(file);  
            
            while (in.available() != 0) {  
                fout.write(in.read());  
            }  
            fout.close();
            
            while (in.available() != 0) {  
                fout.write(in.read());  
            }  
            fout.close();  
        }  
        catch (Exception ex) {  
            ex.printStackTrace();  
        } 
	}

	public static byte[] lerImagemDefault() throws IOException {
		FacesContext contexto = FacesContext.getCurrentInstance();
		InputStream stream = contexto.getExternalContext().getResourceAsStream("//resources//IMGS//imagemDefault.jpg");
		
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();

		int nRead;
		byte[] data = new byte[16384];

		while ((nRead = stream.read(data, 0, data.length)) != -1) {
		  buffer.write(data, 0, nRead);
		}

		buffer.flush();

		byte[] bytes = buffer.toByteArray();
		
		
		return bytes;
	}
}
